package com.kun.admin.authority.controller;

import com.kun.admin.authority.model.MenuItem;
import com.kun.admin.authority.model.RoleInfo;
import com.kun.admin.authority.model.RoleModel;
import com.kun.admin.authority.service.AuthorityMenuService;
import com.kun.admin.authority.service.RoleService;
import com.kun.base.web.model.AjaxResult;
import com.xiaoleilu.hutool.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by likun on 2017/4/1.
 */
@Controller
@RequestMapping("/admin/role")
@Api(description = "角色信息数据接口")
public class AdminRoleController {

    private static Logger logger = LoggerFactory.getLogger(AdminRoleController.class);

    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthorityMenuService menuService;


    @RequestMapping("/findAllRole")
    @ResponseBody
    @ApiOperation(value = "查找所有的角色", response = RoleModel[].class,
            notes = "返回所有查找到的角色信息")
    public List<RoleModel> findAllRole() {
        return roleService.findAll();
    }

    /**
     * 创建角色
     *
     * @param roleModel
     * @return
     */
    @RequestMapping("/create")
    @ResponseBody
    public AjaxResult create(RoleModel roleModel) {
        boolean b = roleService.create(roleModel);
        return new AjaxResult(b);
    }

    /**
     * 修改角色信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editRoleInfo")
    public AjaxResult editRoleInfo(RoleModel roleModel) {
        boolean b = roleService.update(roleModel);
        return new AjaxResult(b);
    }

    @ResponseBody
    @RequestMapping(value = "/delete")
    public AjaxResult delete(Long id) {
        if (id == null) {
            return new AjaxResult(false);
        }
        boolean b = roleService.delete(id);
        return new AjaxResult(b);
    }

    @ResponseBody
    @RequestMapping(value = "/addRoleMenu")
    public AjaxResult addRoleMenu(Long menuId, Long roleId) {
        if (menuId == null || roleId == null) {
            logger.error("给菜单添加权限时数据异常");
            return new AjaxResult(false);
        }
        boolean b = roleService.addRoleMenu(menuId, roleId);
        return new AjaxResult(b);
    }

    @ResponseBody
    @RequestMapping(value = "/deleteRoleMenu")
    public AjaxResult deleteRoleMenu(Long menuId, Long roleId) {
        if (menuId == null || roleId == null) {
            logger.error("移除菜单权限时数据异常");
            return new AjaxResult(false);
        }
        boolean b = roleService.deleteRoleMenu(menuId, roleId);
        return new AjaxResult(b);
    }

    /**
     * 添加角色
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/addRole")
    public AjaxResult addRole(@RequestBody RoleInfo roleInfo) {
        if (StrUtil.isBlank(roleInfo.getName())) {
            return new AjaxResult(false, "角色名称不能为空");
        }
        return new AjaxResult(roleService.addRole(roleInfo));
    }

    @ResponseBody
    @RequestMapping("/updateRole")
    public AjaxResult updateRole(@RequestBody RoleInfo roleInfo) {
        if (StrUtil.isBlank(roleInfo.getName())) {
            return new AjaxResult(false, "角色名称不能为空");
        }
        return new AjaxResult(roleService.updateRole(roleInfo));
    }

    @ResponseBody
    @RequestMapping("/findMenuId")
    public List<Long> findMenuIdByRole(Long roleId) {
        ArrayList<MenuItem> menus = roleService.findMenus(roleId);
        List<Long> menuIds = new ArrayList<>();
        menus.forEach(menu -> menuIds.add(menu.getId()));
        return menuIds;
    }

    /**
     * 删除角色及其权限信息
     * @param roleId
     * @return
     */
    @RequestMapping("/deleteRole")
    @ResponseBody
    public AjaxResult deleteRole(Long roleId){
        if (roleId==null){
            return new AjaxResult(false,"roleId不能为空");
        }
        return new AjaxResult(roleService.deleteRole(roleId));
    }
}